package com.qf.j2105.attendance.service.impl;

import com.qf.j2105.attendance.mapper.LeaveCountMapper;
import com.qf.j2105.attendance.mapper.PunchingMapper;
import com.qf.j2105.attendance.service.LeaveCountService;
import com.qf.j2105.po.LeaveCount;
import com.qf.j2105.po.PunchingRecord;
import com.qf.j2105.vo.AttendanceVO;
import com.qf.j2105.vo.PunchingVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class LeaveCountServiceImpl implements LeaveCountService {

    @Resource
    private LeaveCountMapper leaveCountMapper;

    @Override
    public AttendanceVO showAttendance(PunchingVO punchingVO) {
        int eid = punchingVO.getEid();
        int pmonth = punchingVO.getDate().getMonth();
        int late = 0;//迟到次数
        int seriousLate = 0;//严重迟到次数
        int eralyLeave = 0;//早退次数
        int seriousEralyLeave = 0;//严重早退次数
        int absenteeism = 0;//旷工次数
        int omit = 0;//漏签次数
        int personalLeave = 0;//事假天数
        int illLeave = 0;//病假天数
        int annualLeave = 0;//年假天数
        int marryLeave = 0;//婚丧假天数
        int workOutside = 0;//外勤天数
        int regularCount = getRegularCount(punchingVO.getDate());//当月应该出勤天数

        List<PunchingRecord> punchingList = leaveCountMapper.findPunching(eid,pmonth);
        for (PunchingRecord record:punchingList) {
            Date ptime = new Date(record.getPyear(), record.getPmonth(), record.getPday(), record.getPhour(), record.getPminute(), record.getPsecond());
            Date on = new Date(record.getPyear(), record.getPmonth(), record.getPday(), 8, 0, 0);
            Date off = new Date(record.getPyear(), record.getPmonth(), record.getPday(), 18, 0, 0);
            switch (record.getPunchingType()){
                case "on":
                    int lateTime = (int)Math.floor((double)(ptime.getTime() - on.getTime()) / (60 * 1000));
                    //迟到天数
                    if (0 < lateTime && lateTime< 30) {
                        late += 1;
                    }
                    //严重迟到
                    else if (30 < lateTime && lateTime < 60){
                        seriousLate += 1;
                    }
                    //旷工
                    else if(lateTime > 60){
                        absenteeism += 1;
                    }

                case "off":
                    int lateTime1 = (int)Math.floor((double)(ptime.getTime() - on.getTime()) / (60 * 1000));
                    int earlyTime = (int)Math.floor((double)(off.getTime()-ptime.getTime()) / (60 * 1000));
                    if (0 < earlyTime && earlyTime< 30) {
                        late += 1;
                    }
                    //严重迟到
                    else if (30 < earlyTime && earlyTime < 60){
                        seriousLate += 1;
                    }
                    //旷工
                    else if(earlyTime > 60 && lateTime1 < 60){
                        absenteeism += 1;
                    }
            }
        }
        List<LeaveCount> leaveCountList = leaveCountMapper.findLeaveCountByEid(eid,pmonth);

        return new AttendanceVO(eid,late,seriousLate,eralyLeave,seriousEralyLeave,absenteeism,omit,personalLeave,illLeave,annualLeave,marryLeave,workOutside,regularCount);
    }
    //获取当月应该出勤天数
    public int getRegularCount(Date d){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(d);
        int count = 0;
        int month = Integer.parseInt(date.substring(5, 7));
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DATE, 1);
        while(cal.get(Calendar.MONTH) < month){
            int day = cal.get(Calendar.DAY_OF_WEEK);
            if(!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)){
                count++;
            }
            cal.add(Calendar.DATE, 1);
        }
        return count;
    }
}
